package com.houtai.service.course;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.ResultVo;
import com.houtai.bean.*;
import com.houtai.bean.dto.course.TdCourseDetailDto;
import com.houtai.bean.dto.course.TdCourseListDto;
import com.houtai.bean.dto.course.TdCourseSignUpDto;
import com.houtai.bean.dto.course.collection.TdCourseCollectionInsertDto;
import com.houtai.bean.dto.course.comments.TdCourseCommentsInsertDto;
import com.houtai.bean.dto.course.comments.TdCourseCommentsListDto;
import com.houtai.bean.vo.course.TdCourseDetail;
import com.houtai.bean.vo.course.TdCourseDetailVo;
import com.houtai.bean.vo.course.TdCourseList;
import com.houtai.bean.vo.course.TdCourseListVo;
import com.houtai.bean.vo.course.comments.TdCourseCommentsList;
import com.houtai.bean.vo.course.comments.TdCourseCommentsListVo;
import com.houtai.dao.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {
    private static Logger logger = Logger.getLogger(CourseService.class);
    @Autowired
    private TdCourseDao tdCourseDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TdCourseUserDao tdCourseUserDao;
    @Autowired
    private TdCourseCommentsDao tdCourseCommentsDao;
    @Autowired
    private TdSchoolDao tdSchoolDao;
    @Autowired
    private TdCourseCollectionDao tdCourseCollectionDao;
    /**
     * @Purpose:    活动列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public TdCourseListVo activityList(TdCourseListDto dto) {
        String logInfo = this.getClass().getName() + ":activityList:";
        TdCourseListVo retInfo = new TdCourseListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            String orderBy = "sign_up_size DESC, comments_size desc";

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<TdCourseList> lists = tdCourseDao.findTdCourseList(dto);
            PageInfo<TdCourseList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无在线课程");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    课程详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdCourseDetailVo
     */
    public TdCourseDetailVo activityDetail(TdCourseDetailDto dto, String token) {
        String logInfo = this.getClass().getName() + ":activityDetail:";
        TdCourseDetailVo retInfo = new TdCourseDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdCourseDetail detail = tdCourseDao.findTdCourseDetail(dto.getCourse_id());
            if (detail != null) {
                if (redisTemplate.opsForValue().get(token) != null) {
                    User user = (User)redisTemplate.opsForValue().get(token);
                    TdCourseCollection collection = tdCourseCollectionDao.findTdCourseCollection(user.getUser_id(), detail.getCourse_id());
                    if (collection != null && collection.getState() == 0) {
                        detail.setIs_collection(0);
                    }
                    TdCourseUser courseUser = tdCourseUserDao.findTdCourseUser(user.getUser_id(), detail.getCourse_id());
                    if (courseUser != null) {
                        detail.setIs_apply(0);
                    }
                }
                retInfo.setDetail(detail);
                retInfo.setList(tdCourseDao.findReTdCourseList());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("课程不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    课程报名
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo signUp(TdCourseSignUpDto dto, String token) {
        String logInfo = this.getClass().getName() + ":signUp:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCourseDetail detail = tdCourseDao.findTdCourseDetail(dto.getCourse_id());
            if (detail != null) {

                TdCourseUser courseUser = tdCourseUserDao.findTdCourseUser(user.getUser_id(), detail.getCourse_id());
                if (courseUser != null) {
                    retInfo.setMark("1");
                    retInfo.setTip("已报名该课程");
                } else {
                    courseUser = new TdCourseUser();
                    courseUser.setCourse_id(dto.getCourse_id());
                    courseUser.setUser_id(user.getUser_id());
                    tdCourseUserDao.insertTdCourseUser(courseUser);
                    tdSchoolDao.updateTdSchoolLearningSize(detail.getSchool_id());
                    tdCourseDao.updateTdCourseDetailSignUpSize(detail);
                    retInfo.setMark("0");
                    retInfo.setTip("报名成功");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("课程不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    课程评价
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo comments(TdCourseCommentsInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":comments:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCourseDetail detail = tdCourseDao.findTdCourseDetail(dto.getCourse_id());
            if (detail != null) {
                TdCourseUser courseUser = tdCourseUserDao.findTdCourseUser(user.getUser_id(), detail.getCourse_id());
                if (courseUser != null) {
                    TdCourseComments courseComments = tdCourseCommentsDao.findTdCourseComments(user.getUser_id(), detail.getCourse_id());
                    if (courseComments != null) {
                        retInfo.setMark("1");
                        retInfo.setTip("已评价");
                    } else {
                        courseComments = new TdCourseComments();
                        courseComments.setComments_content(dto.getComments_content());
                        courseComments.setComments_type(dto.getComments_type());
                        courseComments.setCourse_id(dto.getCourse_id());
                        courseComments.setSchool_id(detail.getSchool_id());
                        courseComments.setUser_id(user.getUser_id());
                        tdCourseCommentsDao.insertTdCourseComments(courseComments);

                        int rating = tdCourseCommentsDao.findGoodCourseSize(detail.getCourse_id()) * 10000
                                / tdCourseCommentsDao.findCourseSize(detail.getCourse_id());

                        detail.setRating(rating);
                        if (dto.getComments_type() == 1) {
                            detail.setGood_size(detail.getGood_size() + 1);
                        }
                        if (dto.getComments_type() == 3) {
                            detail.setPoor_size(detail.getPoor_size() + 1);
                        }
                        if (dto.getComments_type() == 2) {
                            detail.setGeneral_size(detail.getGeneral_size() + 1);
                        }
                        tdCourseDao.updateTdCourseDetailRating(detail);

                         int  goods_school_size = tdCourseCommentsDao.findGoodSchoolSize(detail.getSchool_id());
                         int school_size = tdCourseCommentsDao.findSchoolSize(detail.getSchool_id());

                        int school_rating = goods_school_size * 10000
                                / school_size;
                        tdSchoolDao.updateTdSchoolRating(school_rating, detail.getSchool_id());

                        retInfo.setMark("0");
                        retInfo.setTip("评价成功");
                    }

                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("未报名该课程");

                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("课程不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    课程评论列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdCourseCommentsListVo
     */
    public TdCourseCommentsListVo courseCommentsList(TdCourseCommentsListDto dto) {
        String logInfo = this.getClass().getName() + ":courseCommentsList:";
        TdCourseCommentsListVo retInfo = new TdCourseCommentsListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "comments_id DESC");
            List<TdCourseCommentsList> lists = tdCourseCommentsDao.findTdCourseCommentsList(dto);
            PageInfo<TdCourseCommentsList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无评论");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    课程收藏
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo collection(TdCourseCollectionInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":collection:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCourseDetail detail = tdCourseDao.findTdCourseDetail(dto.getCourse_id());
            if (detail != null) {

                TdCourseCollection collection = tdCourseCollectionDao.findTdCourseCollection(user.getUser_id(), detail.getCourse_id());
                if (collection != null) {
                    if (collection.getState() == 0) {
                        retInfo.setMark("1");
                        retInfo.setTip("已收藏");
                    } else {
                        collection.setState(0);
                        tdCourseCollectionDao.updateTdCourseCollectionState(collection);
                        retInfo.setMark("0");
                        retInfo.setTip("收藏成功");
                    }
                } else {
                    collection = new TdCourseCollection();
                    collection.setCourse_id(dto.getCourse_id());
                    collection.setUser_id(user.getUser_id());
                    tdCourseCollectionDao.insertTdCourseCollection(collection);
                    retInfo.setMark("0");
                    retInfo.setTip("收藏成功");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("课程不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    课程取消收藏
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo collectionCancel(TdCourseCollectionInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":collectionCancel:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCourseDetail detail = tdCourseDao.findTdCourseDetail(dto.getCourse_id());
            if (detail != null) {

                TdCourseCollection collection = tdCourseCollectionDao.findTdCourseCollection(user.getUser_id(), detail.getCourse_id());
                if (collection != null) {
                    if (collection.getState() == 0) {
                        collection.setState(1);
                        tdCourseCollectionDao.updateTdCourseCollectionState(collection);
                        retInfo.setMark("0");
                        retInfo.setTip("取消收藏成功");

                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("未收藏");
                    }
                } else {

                    retInfo.setMark("1");
                    retInfo.setTip("未收藏");
                }

            } else {
                retInfo.setMark("1");
                retInfo.setTip("课程不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
